﻿/**
 * @file plic.c
 * @date 23.5.7
 * @author yrz
 * @brief the riscv Platform Level Interrupt Controller(PLIC) 平台级中断控制
 * @version 0.1
 * @status Stable

**/

#include "types.h"
#include "param.h"
#include "memlayout.h"
#include "riscv.h"
#include "defs.h"



void
plicinit(void)
{
	
	// 设置中断请求优先级非0
	*(uint32*)(PLIC + UART0_IRQ * 4) = 1;
	*(uint32*)(PLIC + VIRTIO0_IRQ * 4) = 1;
}

void
plicinithart(void)
{
	int hart = cpuid();


	// 设置使能位，对应uart和虚拟磁盘
	*(uint32*)PLIC_SENABLE(hart) = (1 << UART0_IRQ) | (1 << VIRTIO0_IRQ);


	// 将此hart的s模式优先级阈值设置为0。
	*(uint32*)PLIC_SPRIORITY(hart) = 0;
}


/**
 * @brief 获取目前应该服务的中断请求

*/
int
plic_claim(void)
{
	int hart = cpuid();
	int irq = *(uint32*)PLIC_SCLAIM(hart);
	return irq;
}


/**
 * @brief 告知PLIC正在处理的中断请求

*/
void
plic_complete(int irq)
{
	int hart = cpuid();
	*(uint32*)PLIC_SCLAIM(hart) = irq;
}
